package com.softhis.rekrutacja.server.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.softhis.rekrutacja.shared.dto.UserDTO;

/**
 * @author Sasak
 * 
 */
public class UserDAOImpl implements UserDAO {

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public UserDTO find(int userId) {
		return this.hibernateTemplate.get(UserDTO.class, userId);
	}

	public void save(UserDTO user) {
		this.hibernateTemplate.save(user);
	}

	public void update(UserDTO user) {
		this.hibernateTemplate.update(user);
	}

	public void delete(int userId) {
		UserDTO user = this.hibernateTemplate.get(UserDTO.class, userId);
		this.hibernateTemplate.delete(user);
	}

	@SuppressWarnings("unchecked")
	public List<UserDTO> listUser() {
		return this.hibernateTemplate.find("from UserDTO");
	}
	
	@SuppressWarnings("unchecked")
	public List<UserDTO> findByLogin(String login) {
		List<UserDTO> result = new ArrayList<UserDTO>();
		result.addAll(this.hibernateTemplate.find("from UserDTO u where u.login=?", login));
		return result;
	}

}